<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 100px;
            height: 100px;
            background-color: red;
            position: absolute;
            left: 0;
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <script>
        // 需要通过js 来不停的更改 box 的left 值 ；
        var boxEle = document.querySelector(".box");
        boxEle.onclick = function () {
            // this.style.left = "300px";
            var leftNum = parseInt(getComputedStyle(this)['left']);
            console.log(leftNum);
            var that = this;
            // 通过定时器来更改 位置 
            var timer = setInterval(function () {
                leftNum += 1;
                // console.log(leftNum);
                that.style.left = leftNum + "px";
                // 判断当前方块所在的位置 ；
                console.log(that.offsetLeft) // 方块的偏移量 
                if (that.offsetLeft >= window.innerWidth - that.offsetWidth) {
                    // 停止运动 ：清除定时器
                    clearInterval(timer);
                }

            }, 1);

        }

    </script>
</body>

</html>